package de.fmaul.dicheck.assertions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.fmaul.dicheck.model.DataRow;
import de.fmaul.dicheck.model.TestCase;
import de.fmaul.dicheck.runner.TestResult;

public class RegExpAssertion extends AbstractAssertion {

    private final int column;
    private final Pattern pattern;
    
    public RegExpAssertion(final String message, int newColumn, String newPattern) {
        column = newColumn;
        pattern = Pattern.compile(newPattern);
        setMessage(message);
    }
    
    @Override
    public void checkRow(TestCase testCase, TestResult testResult, DataRow row) {
        
        String value = row.getValues()[column].toString();
        final Matcher matcher = pattern.matcher(value);
        if (!matcher.matches()) {
            fail(testResult);
        }
    }
    
    public int getColumn() {
        return column;
    }
    
    public Pattern getPattern() {
        return pattern;
    }
    

}
